package com.fivetalks.client;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.fivetalks.object.Member;
import com.fivetalks.object.RoomInfo;

public class PreChatActivity extends Activity implements Runnable,
		OnClickListener {

	Member member;
	RoomInfo room;
	Thread m_thread;
	ImageButton btJoin,btOut;
	long roomNumber;
	long leader;
	ImageView ibUser,ibUser2;
	TextView tvUser,tvUser2;

	ImageButton btStart;

	Handler mHandler;

	boolean isRun,isOut;
	String goCall, goStart;
	Bitmap bm;
	
	String title;

	TextView tvRoomTitle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pre_chat);
		tvRoomTitle = (TextView)findViewById(R.id.tvRoomTitle);
		isRun = true;
		isOut = true;
		goStart = "NO";

		Intent intent = getIntent();
		roomNumber = (Long) intent.getSerializableExtra("roomNumber");
		btStart = (ImageButton) findViewById(R.id.btStart);
		btStart.setOnClickListener(this);
		
		btOut = (ImageButton) findViewById(R.id.btOut);
		btOut.setOnClickListener(this);
		// 방목록 사용자 등록
		Thread m_thread_init = new Thread(new Runnable() {
			@Override
			public void run() {
				// 해당 작업을 처리함
				SocketClient sc = SocketClient.getInstance(null, null,
						PreChatActivity.this, null, null, null,null,null);
				try {
					sc.getDos().writeUTF("enterReadyRoom");
					sc.getDos().writeUTF(roomNumber + "");
					Log.d("prechat", "roomNumber : " + roomNumber);
					sc.getDos().flush();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});

		m_thread_init.start();
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if(msg.what == 0)
				{
					Log.d("prechat","이제 그만");
					Intent intent = new Intent(PreChatActivity.this, ChattingActivity.class);
					intent.putExtra("roomNumber", roomNumber);
					startActivity(intent);
				}
				else if(msg.what == 1)
				{
					SocketClient sc = SocketClient.getInstance(null, null,
							PreChatActivity.this, null, null, null,null,null);
					Log.d("prechat","나가자");
					Intent intent = new Intent(PreChatActivity.this,
							MainFrameActivity.class);
					intent.putExtra("email", sc.getEmail());
					intent.putExtra("password", sc.getPassword());
					startActivity(intent);
				}
				else
				{
					if(isRun!=false)
					{
						Log.d("prechat","핸들러 실행");
						start();
						
						mHandler.sendEmptyMessageDelayed(2, 2000);
				//		mHandler.sendEmptyMessage(1);
					}
					else
					{
						if(isOut!=false) {
							mHandler.sendEmptyMessage(0);
						}
						else {
							mHandler.sendEmptyMessage(1);
						}
						 
					}
				}
			}
		};
		

	}

	@Override
	protected void onResume() {
		super.onResume();
		mHandler.sendEmptyMessage(2);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	public void start() {
		m_thread = new Thread(this);
		m_thread.start();
	}

	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.btStart:
			Thread m_thread_go = new Thread(new Runnable() {
				@Override
				public void run() {
					// 해당 작업을 처리함
					SocketClient sc = SocketClient.getInstance(null, null,
							PreChatActivity.this, null, null, null,null,null);
					try {
						sc.getDos().writeUTF("sendGo");
						sc.getDos().writeUTF(roomNumber + "");
						Log.d("prechat", "go 전송");
						sc.getDos().flush();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			});

			m_thread_go.start();
			break;
			
		case R.id.btOut :
			isRun=false;
			isOut=false;
			Thread m_thread_out = new Thread(new Runnable() {
				@Override
				public void run() {
					// 해당 작업을 처리함
					SocketClient sc = SocketClient.getInstance(null, null,
							PreChatActivity.this, null, null, null,null,null);
					try {
						sc.getDos().writeUTF("roomOut");
						sc.getDos().writeUTF(roomNumber + "");
						Log.d("prechat", "roomOut 전송1111");
						sc.getDos().flush();
					} catch (IOException e) {
						e.printStackTrace();
					}

				}
			});

			m_thread_out.start();
			
			
			
			
			
			break;
			
		}
	}

	public void run() {
		SocketClient sc = SocketClient.getInstance(null, null,
				PreChatActivity.this, null, null, null,null,null);

			try {
				sc.getDos().writeUTF("refreshReadyRoom");
				sc.getDos().writeUTF(roomNumber + "");

				sc.getDos().flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
	}
}
